<?php
	####################################################
	# Pagenation 리스트 페이지네이션 함수              #
	####################################################

	class Pagenation {

		# 페이지네이션
		#
		# example)
		#	$pagenation_cfg = array(
		#		'uri'				=> "ncrud_list.php",
		#		'articles_count'	=> 20,
		#		'req_page'			=> empty($req['p1'])? 0 : $req['p1'],
		#		'page_size'			=> 6,
		#		'articles_cnt'		=> $db->getcnts($ncrud['table_name']),
		#		'style'				=> Pagenation::$style_default,
		#		'get'				=> 'p1'
		#	);
		#	$article = $db->gets(
		#		sprintf("SELECT * FROM `%s` ORDER BY buid DESC LIMIT %s, %s ",
		#			$ncrud['table_name'],
		#			$pagenation_cfg['req_page'] * $pagenation_cfg['articles_count'],
		#			$pagenation_cfg['articles_count']
		#		)
		#	);
		#	$pagenation = Pagenation::out($pagenation_cfg);
		#
		#


		public static $style_default = "
			<style type=\"text/css\">
				.pagenation_prev{padding: 0 5px 0 5px;height:20px;margin:2px;cursor:pointer;}
				.pagenation_curr{padding: 0 5px 0 5px;height:20px;margin:2px;font-weight:bold;}
				.pagenation_link{padding: 0 5px 0 5px;height:20px;margin:2px;cursor:pointer;}
				.pagenation_next{padding: 0 5px 0 5px;height:20px;margin:2px;cursor:pointer;}
			</style>
		";

		public static $style_white = "
			<style type=\"text/css\">
				.pagenation_prev{background-color:#F1F1F1;padding: 2px 10px 2px 10px;height:30px;border:1px solid #C2C4CA;margin:2px;color:#999999;cursor:pointer;}
				.pagenation_curr{background-color:#FFFFFF;padding: 2px 10px 2px 10px;height:30px;border:1px solid #C2C4CA;margin:2px;color:#484848;font-weight:bold;}
				.pagenation_link{background-color:#F1F1F1;padding: 2px 10px 2px 10px;height:30px;border:1px solid #C2C4CA;margin:2px;color:#999999;cursor:pointer;}
				.pagenation_next{background-color:#F1F1F1;padding: 2px 10px 2px 10px;height:30px;border:1px solid #C2C4CA;margin:2px;color:#999999;cursor:pointer;}
			</style>
		";

		public static function out($pagenation_cfg){

			$uri				= $pagenation_cfg['uri'];            # 이동할 URL
			$articles_page		= $pagenation_cfg['articles_count']; # 한페이지에 보여줄 레코드 수
			$page				= $pagenation_cfg['req_page'];       # 현재 페이지
			$total				= $pagenation_cfg['articles_cnt'];   # 한페이지에 보여줄 갯수
			$page_size			= $pagenation_cfg['page_size'];      # 전체 레코드 수
			$style				= $pagenation_cfg['style'];          # 스타일
			$get				= $pagenation_cfg['get'];            # 기준 $_GET키 이름

			$prt_first_page = NULL;
			$pt_page = NULL;
			$prt_end_page = NULL;

			if(strpos($uri,'?') === false) {
				$uri .= "?";
			} else {
				$uri .= "&";
			}

			//echo $uri;
			//echo $get;
			if(strpos($uri,$get) === true){
				//echo $uri;
			}

			if($total > 0){
				$total_page = intval(($total-1)/$articles_page);
				if (!$page) {
					$now_page = 0;
				} else {
					$now_page = $page;
				}
				$start_rec = ($now_page) * $articles_page;
				if (($start_rec+$articles_page)>$total) {
					$end_rec = $total;
				} else {
					$end_rec = $start_rec + $articles_page;
				}
				$first_page = intval($now_page/$page_size)*$page_size;

				if (($first_page+($page_size-1))>$total_page) {
					$last_page = $total_page;
				} else {
					$last_page = $first_page+($page_size-1);
				}

				if($first_page == 0) {
					$pt_page = "";
				} else {
					$prt_first_page = sprintf("<a href='%s%s=0'>[1]</a>...",$uri,$get);
					$prev_page = $first_page - 1;
					$pt_page = sprintf("<a href='%s%s=%s'><span class='pagenation_prev'>◀ %s</span></a> ",$uri,$get,$prev_page, '이전');
				}

				for ($j=$first_page; $j<=$last_page; $j++) {
					$dis_page = $j + 1;
					if ($j == $now_page) {
						$pt_page .= sprintf("<span class='pagenation_curr'>%s</span>",$dis_page);
					} else {
						$pt_page .= sprintf("<a href='%s%s=%s'><span class='pagenation_link'>%s</span></a>",$uri,$get,$j,$dis_page);
					}
				}
				if ($last_page != $total_page) {
					$next_page = $first_page+$page_size;
					$pt_page .= sprintf(" <a href='%s%s=%s'><span class='pagenation_next'>%s ▶</span></a>",$uri,$get,$next_page, '다음');
					$prt_end_page = sprintf("...<a href='%s%s=%s'>[%s]</a>",$uri,$total_page,$total_page+1);
				} else {
					$pt_page .= "";
				}


				$dis_now_page = $now_page + 1;
				$dis_total_page = $total_page + 1;

				$pt_page = sprintf("%s<table width=\"100%%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"center\" style=\"text-align:center\">%s</td></tr></table>", $style, $prt_first_page.$pt_page.$prt_end_page);
		//		return $prt_first_page.$pt_page.$prt_end_page;
				return $pt_page;
			}
		}
	}

?>